package com.scopionstudio.bean;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.scopionstudio.utils.RessItems;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class RssHandles extends DefaultHandler {
	private static final String TITLE = "title";
	private static final String ITEM = "item";
	private static final String DESCRIPTION = "description";
	private static final String LINK = "link";
	private static final String  SUMMARYIMG = "summaryImg";
	private static final String PUBDATE = "pubdate";
	private boolean checkStart;
	RessItems item;
	private ArrayList<RessItems> itemList;
	private int startSrcIma, endSrcIma;
	private int startStrInfo;
	private String strInfo;
	private int endInfo;
	private int indexPlace;
	private int strInfoLength;
	private String strTemp;
	private ArrayList<String> arlStrInfo = new ArrayList<String>();
	private StringBuilder sBuilder;

	public RssHandles() {
		itemList = new ArrayList<RessItems>();

	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		super.characters(ch, start, length);
		if (checkStart && (sBuilder != null)) {
			sBuilder.append(ch, start, length);
		}
	}

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.startDocument();
		itemList = new ArrayList<RessItems>();
		sBuilder = new StringBuilder();

	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		// String a = localName;
		if (localName.equalsIgnoreCase(ITEM)) {
			item = new RessItems();
			checkStart = true;
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		super.endElement(uri, localName, qName);
		if (localName.equalsIgnoreCase(ITEM)) {
			itemList.add(item);
			checkStart = false;
		} else if (checkStart == true) {
			if (localName.equalsIgnoreCase(TITLE) && checkStart) {
				item.setTitle(sBuilder.toString().trim());
			} else if (localName.equalsIgnoreCase(DESCRIPTION) && checkStart) {
				// item.setDescription(sBuilder.toString().trim());
				strInfo = sBuilder.toString().trim();
				startSrcIma = strInfo.indexOf("src=");
				if (startSrcIma != -1) {
					endSrcIma = strInfo.indexOf("\"", startSrcIma + 6);
					item.setStrUrlImage(strInfo.substring(startSrcIma + 5,
							endSrcIma));
					/*Bitmap imgbit = getBitmapFromURL(strInfo.substring(
							startSrcIma + 5, endSrcIma));
					item.setbImg(imgbit);*/
				}
				strInfoLength = strInfo.length();
				for (int indexPlace = 0; indexPlace < strInfoLength;) {
					startStrInfo = strInfo.indexOf(">", indexPlace);
					endInfo = strInfo.indexOf("<", startStrInfo);
					if (endInfo == -1)
						endInfo = strInfoLength;
					strTemp = strInfo.substring(startStrInfo + 1, endInfo);
					if (null != strTemp)
						arlStrInfo.add(strTemp);
					indexPlace = endInfo;
				}
				strTemp = "";
				for (indexPlace = 0; indexPlace < arlStrInfo.size(); indexPlace++) {
					strTemp = strTemp.concat(arlStrInfo.get(indexPlace));
				}
				arlStrInfo.clear();

				// item.setDescription("" +
				// StringEscapeUtils.unescapeHtml4(strTemp));

			} else if (localName.equalsIgnoreCase(LINK) && checkStart) {
				item.setLink(sBuilder.toString().trim());
			} else if (localName.equalsIgnoreCase(PUBDATE) && checkStart) {
				item.setPubdate(sBuilder.toString().trim());
			}else if(localName.equalsIgnoreCase(SUMMARYIMG) && checkStart){
				Bitmap imgbit = getBitmapFromURL("http://hcm.24h.com.vn/"+ sBuilder.toString().trim());
				item.setbImg(imgbit);
			}
			// itemList.add(item);
			sBuilder.setLength(0);
		}
	}

	public ArrayList<RessItems> getItemList() {
		return itemList;
	}

	public static Bitmap getBitmapFromURL(String src) {
		try {
			// Log.e("src",src);
			URL url = new URL(src);
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			connection.setDoInput(true);
			connection.connect();
			InputStream input = connection.getInputStream();
			Bitmap myBitmap = BitmapFactory.decodeStream(input);
			// Log.e("Bitmap","returned");
			return myBitmap;
		} catch (IOException e) {
			e.printStackTrace();
			Log.e("Exception", e.getMessage());
			return null;
		}
	}
}
